/*action create by pionner code builder*/

package com.pioneer.app.dev.department;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.hibernate.Query;
import org.hibernate.Transaction;

import config.BaseHibernateDAO;

public class DepartmentDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(Department.class);
	public Department findById(java.lang.Integer id){
		log.debug("getting Department instance with id: " + id);
		try {
			Department instance =(Department)getSession().get("com.pioneer.app.dev.department.Department",id);
			return instance;
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void delete(Department persistentInstance){
		log.debug("deletting Department persistentInstance  ");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void add(Department persistentInstance){
		log.debug("deletting Department persistentInstance  ");
		try {
			getSession().save(persistentInstance);
			log.debug("add successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void update(Department persistentInstance){
		log.debug("deletting Department persistentInstance  ");
		try {
			getSession().update(persistentInstance);
			log.debug("update successful");
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findByCondition(String condition){
		log.debug("finding by condition "+condition);
		try {

			String queryString =null;
			if(null!=condition){
				queryString = "from Department where "+condition;
			}else{queryString = "from Department";
			}

			Query queryObject = getSession().createQuery(queryString);
			queryObject.setMaxResults(500);
			return queryObject.list();
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public void deleteObjs(java.lang.Integer[]ids){
		log.debug("deleting by objects ");
		Transaction tx=this.getSession().beginTransaction();
		try {
			if(null!=ids){
				Department obj=null;
				java.lang.Integer id=null;
				for(int i=0;i<ids.length;i++){
					/*id=ids[i];
					obj=new Department();*/
					
					id=ids[i];
					obj=this.findById(id);
					//obj.setId(id);
					this.delete(obj);
				}
				tx.commit();
			}
		}catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public Document getTreeNode(String pidStr){
	Document doc=null;
	try{
		if(null==pidStr){
			pidStr="-1";
		}
		List objs=this.findByCondition("pid="+pidStr);
		doc=DocumentFactory.getInstance().createDocument();
		Element root=doc.addElement("TREENODES");
		root.setAttributeValue("pid", pidStr);
		Department obj=null;
		Iterator it=objs.iterator();
		Element elt=null;
		while(it.hasNext()){
			obj=(Department)it.next();
			elt=root.addElement("TREENODE");
			elt.addAttribute("id", ""+obj.getId().intValue());
			elt.addAttribute("name", obj.getName());
			
		}
	}catch(Exception e){
	}
	return doc;
	}

}
